import React, {FC, useEffect, useRef, useState} from 'react';
import {
  View,
  Text,
  StyleSheet,
  ScrollView,
  TouchableOpacity,
  Image,
} from 'react-native';
import icon_arrow from '../../../assets/icon_arrow.png';
import HomeStore from '../../../stores/HomeStore';
import CategoryModal, {CategoryModalRed} from './CategoryModal';
import {observer} from 'mobx-react';

type PropsType = {};
const Header: FC<PropsType> = observer(() => {
  const [tabIndex, setTabIndex] = useState<number>(0);
  useEffect(() => {
    HomeStore.getCategoryList();
  }, []);
  const myList = HomeStore.categoryList.filter(item => item.isAdd);
  const categoryModalRef = useRef<CategoryModalRed>(null);
  const onOpenPress = () => {
    categoryModalRef.current?.show();
  };
  return (
    <View style={styles.root}>
      <ScrollView style={styles.scrollView} horizontal={true}>
        {myList.map((item, index) => {
          return (
            <TouchableOpacity
              key={`${item.name}`}
              style={styles.tabItem}
              onPress={() => setTabIndex(index)}>
              <Text
                style={
                  tabIndex === index
                    ? styles.tabItemTxtSelected
                    : styles.tabItemTxt
                }>
                {item.name}
              </Text>
            </TouchableOpacity>
          );
        })}
      </ScrollView>
      <TouchableOpacity style={styles.openBtn} onPress={onOpenPress}>
        <Image source={icon_arrow} style={styles.img} />
      </TouchableOpacity>
      <CategoryModal ref={categoryModalRef} />
    </View>
  );
});

const styles = StyleSheet.create({
  root: {
    width: '100%',
    height: 36,
    backgroundColor: 'white',
    flexDirection: 'row',
    alignItems: 'center',
  },
  scrollView: {
    flex: 1,
    height: '100%',
  },
  tabItem: {
    width: 52,
    height: '100%',
    justifyContent: 'center',
    alignItems: 'center',
  },
  tabItemTxt: {
    fontSize: 16,
    color: '#999',
  },
  tabItemTxtSelected: {
    fontSize: 18,
    color: '#333',
  },
  img: {
    width: 18,
    height: 18,
    resizeMode: 'contain',
    transform: [{rotate: '270deg'}],
  },
  openBtn: {
    width: 40,
    height: '100%',
    justifyContent: 'center',
    alignItems: 'center',
  },
});
export default Header;
